package com.cbtx.module_sp.ui.activity

import android.Manifest
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import com.cbtx.module_sp.MyApplication
import com.cbtx.module_sp.R
import com.cbtx.module_sp.ui.activity.base.MyBaseVMActivity
import com.cbtx.module_sp.ui.vm.SplashActivityVM
import com.tbruyelle.rxpermissions2.RxPermissions
import com.txcb.lib.base.utils.LoadingDialogUtil
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.splash_activity.*


class SplashActivity : MyBaseVMActivity<SplashActivityVM>() {

    var isNeedShowNetDialog = true
    private val mHandlerTime = Handler()

    override fun getPresenterClazz(): Class<SplashActivityVM> {
        return SplashActivityVM::class.java
    }

    override fun getLayoutId(): Int {

        return R.layout.splash_activity
    }

    override fun initView(savedInstanceState: Bundle?) {
        mTvDeviceCode.text = "云屏编码:${MyApplication.mAndroidId}"

        //拍照要的权限
        val permission = arrayOf(
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION
        )

        RxPermissions(this@SplashActivity)
            .request(*permission)
            .subscribe(Consumer<Boolean?> { aBoolean ->
                if (!aBoolean!!) {
                    Toast.makeText(this@SplashActivity, "  权限不足", Toast.LENGTH_LONG).show()
                } else {
                    LoadingDialogUtil.showDialog(mActivity, "")
                    checkNet()
                }
            })


        /**
         * 定时获取是否成功注册
         */
//        var mTimer = Timer()
//        mTimer.schedule(object : TimerTask() {
//            override fun run() {
//                // 定时获取是否成功注册
//            }
//        }, 5 * 1000, 5 * 1000)
//        mTimer.cancel()

        // 展示二维码
        mVM.qrCodeBitmap.observe(this, Observer {
            iv_qr_code_display.setImageBitmap(it)
        })

        var a = true
        // 设备是否认证
        mVM.isAuthorization.observe(this, Observer {
            if (it) {
                //获取数据后，允许进入首页
                MainActivity.start(this)
                finish()
            } else {
                if(a){
//                    mVM.loginRq()
                    a = false
                }
//                mVM.loginRq()
//                mVM.requestToken()
            }
            LoadingDialogUtil.closeDialog()
        })
        startTime()
    }

    override fun onMyViewClick(view: View) {

    }

    override fun onResume() {
        super.onResume()
        checkNet()
    }

    private fun startTime() {
        mHandlerTime.postDelayed({
            checkNet()
        }, 1000 * 5)
    }


    private fun checkNet() {
        if (!isNetworkConnected(mActivity)) {
            if (isNeedShowNetDialog) {
                isNeedShowNetDialog = false
                showNoNet(true)
            }
            startTime()
        } else {
            mVM.isAuthorization()
            showNoNet(false)
        }
    }

    override fun getData() {

    }

    override fun onBackPressed() {

    }
}